Class ParseContext

java.lang.Object
org.dellroad.stuff.string.ParseContext
All Implemented Interfaces:
Serializable, Cloneable

public class ParseContext extends Object implements Cloneable, Serializable
Utility class supporting parsing of strings.

Instances of this class are not thread safe.

See Also:
  • Constructor Details

    • ParseContext

      public ParseContext(String input)
      Constructor.
      Parameters:
      input - the input string to parse
      Throws:
      IllegalArgumentException - if input is null
  • Method Details

    • getOriginalInput

      public String getOriginalInput()
      Get the original input string as passed to the constructor.
      Returns:
      original input string
    • getInput

      public String getInput()
      Get the current input.
      Returns:
      substring of the original input string starting at the current parse position
    • getIndex

      public int getIndex()
      Get the current index into the original input string.
      Returns:
      current parse position
      See Also:
    • setIndex

      public void setIndex(int index)
      Set the current index into the original input string.
      Parameters:
      index - new parse position
      Throws:
      IllegalArgumentException - if index is greater than the original string length
      See Also:
    • reset

      public void reset()
      Reset this instance. This instance will return to the state it was in immediately after construction.

      This method just invokes:

      setIndex(0)
    • matchPrefix

      public Matcher matchPrefix(String regex)
      Match the current input against the given regular expression and advance past it.
      Parameters:
      regex - regular expression to match against the current input
      Returns:
      Matcher for the successfully matched input
      Throws:
      IllegalArgumentException - if the current input does not match
    • matchPrefix

      public Matcher matchPrefix(Pattern regex)
      Match the current input against the given regular expression and advance past it.
      Parameters:
      regex - regular expression to match against the current input
      Returns:
      Matcher for the successfully matched input
      Throws:
      IllegalArgumentException - if the current input does not match
    • tryLiteral

      public boolean tryLiteral(String prefix)
      Determine if the current input starts with the given literal prefix. If so, advance past it. If not, do not advance.
      Parameters:
      prefix - literal string to try to match against the current input
      Returns:
      whether the current input matched prefix
    • tryPattern

      public Matcher tryPattern(String pattern)
      Determine if the current input starts with the given regular expression. If so, advance past it and return a successful Matcher; otherwise, return null.
      Parameters:
      pattern - regular expression to match against input
      Returns:
      match if successful, null if not
    • tryPattern

      public Matcher tryPattern(Pattern regex)
      Determine if the current input starts with the given regular expression. If so, advance past it and return a successful Matcher; otherwise, return null.
      Parameters:
      regex - regular expression to match against input
      Returns:
      match if successful, null if not
    • isEOF

      public boolean isEOF()
      Determine if we are at the end of the input.
      Returns:
      true if end of input has been reached
    • read

      public char read()
      Read and advance past the next character.
      Returns:
      the next character of input
      Throws:
      IllegalArgumentException - if there are no more characters
    • peek

      public char peek()
      Read, but do not advance past, the next character.
      Returns:
      the next character of input
      Throws:
      IllegalArgumentException - if there are no more characters
    • unread

      public void unread()
      Push back the previously read character.
      Throws:
      IllegalStateException - if the beginning of the original string has been reached
    • expect

      public void expect(char ch)
      Read and advance past the next character, which must match ch.
      Parameters:
      ch - the expected next character of input
      Throws:
      IllegalArgumentException - if there are no more characters or the next character read is not ch
    • skipWhitespace

      public void skipWhitespace()
      Skip leading whitespace, if any.
      See Also:
    • clone

      public ParseContext clone()
      Clone this instance.
      Overrides:
      clone in class Object
      Returns:
      cloned instance
    • buildException

      public IllegalArgumentException buildException()
      Create a generic exception for rejecting the current input.
      Returns:
      rejection exception object
    • buildException

      public IllegalArgumentException buildException(String message)
      Create an exception for rejecting the current input.
      Parameters:
      message - problem description, or null for none
      Returns:
      corresponding exception object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object