Package org.dellroad.stuff.string
Class ParseContext
java.lang.Object
org.dellroad.stuff.string.ParseContext
- All Implemented Interfaces:
Serializable
,Cloneable
Utility class supporting parsing of strings.
Instances of this class are not thread safe.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionCreate a generic exception for rejecting the current input.buildException
(String message) Create an exception for rejecting the current input.clone()
Clone this instance.boolean
void
expect
(char ch) Read and advance past the next character, which must matchch
.int
getIndex()
Get the current index into the original input string.getInput()
Get the current input.Get the original input string as passed to the constructor.int
hashCode()
boolean
isEOF()
Determine if we are at the end of the input.matchPrefix
(String regex) Match the current input against the given regular expression and advance past it.matchPrefix
(Pattern 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
Skip leading whitespace, if any.boolean
tryLiteral
(String prefix) Determine if the current input starts with the given literal prefix.tryPattern
(String pattern) Determine if the current input starts with the given regular expression.tryPattern
(Pattern regex) Determine if the current input starts with the given regular expression.void
unread()
Push back the previously read character.
-
Constructor Details
-
ParseContext
Constructor.- Parameters:
input
- the input string to parse- Throws:
IllegalArgumentException
- ifinput
is null
-
-
Method Details
-
getOriginalInput
Get the original input string as passed to the constructor.- Returns:
- original input 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
- ifindex
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
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
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
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
Determine if the current input starts with the given regular expression. If so, advance past it and return a successfulMatcher
; otherwise, return null.- Parameters:
pattern
- regular expression to match against input- Returns:
- match if successful, null if not
-
tryPattern
Determine if the current input starts with the given regular expression. If so, advance past it and return a successfulMatcher
; 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 matchch
.- Parameters:
ch
- the expected next character of input- Throws:
IllegalArgumentException
- if there are no more characters or the next character read is notch
-
skipWhitespace
public void skipWhitespace()Skip leading whitespace, if any.- See Also:
-
clone
Clone this instance. -
buildException
Create a generic exception for rejecting the current input.- Returns:
- rejection exception object
-
buildException
Create an exception for rejecting the current input.- Parameters:
message
- problem description, ornull
for none- Returns:
- corresponding exception object
-
equals
-
hashCode
public int hashCode()
-