org.dellroad.stuff.string
Class ParseContext

java.lang.Object
  extended by org.dellroad.stuff.string.ParseContext
All Implemented Interfaces:
Cloneable

public class ParseContext
extends Object
implements Cloneable

Utility class supporting parsing of strings.

Instances of this class are not thread safe.


Constructor Summary
ParseContext(String input)
          Constructor.
 
Method Summary
 IllegalArgumentException buildException()
          Create a generic exception for rejecting the current input.
 IllegalArgumentException buildException(String message)
          Create an exception for rejecting the current input.
 ParseContext clone()
          Clone this instance.
 void expect(char ch)
          Read and advance past the next character, which must match ch.
 int getIndex()
          Get the current index into the original input string.
 String getInput()
          Get the current input.
 String getOriginalInput()
          Get the original input string as passed to the constructor.
 boolean isEOF()
          Determine if we are at the end of the input.
 Matcher matchPrefix(Pattern regex)
          Match the current input against the given regular expression and advance past it.
 Matcher matchPrefix(String regex)
          Match the current input against the given regular expression and advance past it.
 char peek()
          Read, but do not advance past, the next character.
 char read()
          Read and advance past the next character.
 void reset()
          Reset this instance.
 void setIndex(int index)
          Set the current index into the original input string.
 void skipWhitespace()
          Skip leading whitespace, if any.
 boolean tryLiteral(String prefix)
          Determine if the current input starts with the given literal prefix.
 void unread()
          Push back the previously read character.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ParseContext

public ParseContext(String input)
Constructor.

Parameters:
input - the input string to parse
Method Detail

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(int)

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:
getIndex()

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
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
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

isEOF

public boolean isEOF()
Determine if we are at the end of the input.


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:
Character.isWhitespace(char)

clone

public ParseContext clone()
Clone this instance.

Overrides:
clone in class Object

buildException

public IllegalArgumentException buildException()
Create a generic exception for rejecting the current input.


buildException

public IllegalArgumentException buildException(String message)
Create an exception for rejecting the current input.

Parameters:
message - problem description, or null for none