public class SimpleTextParser extends Object
Constructor and Description |
---|
SimpleTextParser(CharReadBuffer buffer)
Construct a new instance that reads characters from the given character buffer.
|
SimpleTextParser(Reader reader)
Construct a new instance that reads characters from the given reader.
|
Modifier and Type | Method and Description |
---|---|
int |
choose(List<String> expected)
Return the index of the argument that exactly matches the
current token . |
int |
choose(String... expected)
Return the index of the argument that exactly matches the
current token . |
int |
chooseIgnoreCase(List<String> expected)
Return the index of the argument that matches the
current token ,
ignoring case. |
int |
chooseIgnoreCase(String... expected)
Return the index of the argument that matches the
current token ,
ignoring case. |
SimpleTextParser |
consume(int len,
IntConsumer consumer)
Consume at most
len characters from the stream, passing each to the given consumer. |
SimpleTextParser |
consume(IntPredicate pred,
IntConsumer consumer)
Consume characters from the stream and pass them to
consumer while the given predicate
returns true. |
SimpleTextParser |
consumeWithLineContinuation(char lineContinuationChar,
int len,
IntConsumer consumer)
Consume at most
len characters from the stream, passing each to the given consumer. |
SimpleTextParser |
consumeWithLineContinuation(char lineContinuationChar,
IntPredicate pred,
IntConsumer consumer)
Consume characters from the stream and pass them to
consumer while the given predicate
returns true. |
SimpleTextParser |
discard(int len)
Discard
len number of characters from the character stream. |
SimpleTextParser |
discard(IntPredicate pred)
Discard characters from the stream while the given predicate returns true.
|
SimpleTextParser |
discardLine()
Discard all remaining characters on the current line, including the terminating
newline character sequence.
|
SimpleTextParser |
discardLineWhitespace()
Discard the next whitespace characters on the current line.
|
SimpleTextParser |
discardNewLineSequence()
Discard the newline character sequence at the current reader position.
|
SimpleTextParser |
discardWhitespace()
Discard a sequence of whitespace characters from the character stream starting from the
current parser position.
|
SimpleTextParser |
discardWithLineContinuation(char lineContinuationChar,
int len)
Discard
len number of characters from the character stream. |
SimpleTextParser |
discardWithLineContinuation(char lineContinuationChar,
IntPredicate pred)
Discard characters from the stream while the given predicate returns true.
|
int |
getColumnNumber()
Get the current column number.
|
String |
getCurrentToken()
Get the current token.
|
double |
getCurrentTokenAsDouble()
Get the current token parsed as a double.
|
int |
getCurrentTokenAsInt()
Get the current token parsed as an integer.
|
int |
getCurrentTokenColumnNumber()
Get the column position that the current token started on.
|
int |
getCurrentTokenLineNumber()
Get the line number that the current token started on.
|
int |
getLineNumber()
Get the current line number.
|
int |
getMaxStringLength()
Get the maximum length for strings returned by this instance.
|
boolean |
hasMoreCharacters()
Return true if there are more characters to read from this instance.
|
boolean |
hasMoreCharactersOnLine()
Return true if there are more characters to read on the current line.
|
boolean |
hasNonEmptyToken()
Return true if the current token is not null or empty.
|
static boolean |
isAlphanumeric(int ch)
Return true if the given character (Unicode code point) is alphanumeric.
|
static boolean |
isDecimalPart(int ch)
Return true if the given character (Unicode code point) can be used as part of
the string representation of a decimal number.
|
static boolean |
isIntegerPart(int ch)
Return true if the given character (Unicode code point) can be used as part of
the string representation of an integer.
|
static boolean |
isLineWhitespace(int ch)
Return true if the given character (Unicode code point) is whitespace
that is not used in newline sequences (ie, not '\r' or '\n').
|
static boolean |
isNewLinePart(int ch)
Return true if the given character (Unicode code point) is used
as part of newline sequences (ie, is either '\r' or '\n').
|
static boolean |
isNotAlphanumeric(int ch)
Return true if the given character (Unicode code point) is not alphanumeric.
|
static boolean |
isNotNewLinePart(int ch)
Return true if the given character (Unicode code point) is not used as
part of newline sequences (ie, not '\r' or '\n').
|
static boolean |
isNotWhitespace(int ch)
Return true if the given character (Unicode code point) is not whitespace.
|
static boolean |
isWhitespace(int ch)
Return true if the given character (Unicode code point) is whitespace.
|
SimpleTextParser |
match(String expected)
Compare the
current token with the argument and throw an
exception if they are not equal. |
SimpleTextParser |
matchIgnoreCase(String expected)
Compare the
current token with the argument and throw an
exception if they are not equal. |
SimpleTextParser |
next(int len)
Read a string containing at most
len characters from the stream and
set it as the current token. |
SimpleTextParser |
next(IntPredicate pred)
Read characters from the stream while the given predicate returns true and set the result
as the current token.
|
SimpleTextParser |
nextAlphanumeric()
Read a sequence of alphanumeric characters starting from the current parser position
and set the result as the current token.
|
SimpleTextParser |
nextLine()
Read characters from the current parser position to the next new line sequence and
set the result as the current token .
|
SimpleTextParser |
nextWithLineContinuation(char lineContinuationChar,
int len)
Read a string containing at most
len characters from the stream and
set it as the current token. |
SimpleTextParser |
nextWithLineContinuation(char lineContinuationChar,
IntPredicate pred)
Read characters from the stream while the given predicate returns true and set the result
as the current token.
|
IllegalStateException |
parseError(int line,
int col,
String msg)
Return an exception indicating an error during parsing.
|
IllegalStateException |
parseError(int line,
int col,
String msg,
Throwable cause)
Return an exception indicating an error during parsing.
|
IllegalStateException |
parseError(String msg)
Return an exception indicating an error occurring at the current parser position.
|
IllegalStateException |
parseError(String msg,
Throwable cause)
Return an exception indicating an error occurring at the current parser position.
|
String |
peek(int len)
Return a string containing containing at most
len characters from the stream but
without changing the parser position. |
String |
peek(IntPredicate pred)
Read characters from the stream while the given predicate returns true but do not
change the current token or advance the parser position.
|
int |
peekChar()
Return the next character in the stream but do not advance the parser position.
|
int |
readChar()
Read and return the next character in the stream and advance the parser position.
|
void |
setColumnNumber(int column)
Set the current column number.
|
void |
setLineNumber(int lineNumber)
Set the current line number.
|
void |
setMaxStringLength(int maxStringLength)
Set the maximum length for strings returned by this instance.
|
IllegalStateException |
tokenError(String msg)
Get an exception indicating an error during parsing at the current token position.
|
IllegalStateException |
tokenError(String msg,
Throwable cause)
Get an exception indicating an error during parsing at the current token position.
|
int |
tryChoose(List<String> expected)
Return the index of the argument that exactly matches the
current token
or -1 if no match is found. |
int |
tryChoose(String... expected)
Return the index of the argument that exactly matches the
current token
or -1 if no match is found. |
int |
tryChooseIgnoreCase(List<String> expected)
Return the index of the argument that matches the
current token
or -1 if no match is found. |
int |
tryChooseIgnoreCase(String... expected)
Return the index of the argument that matches the
current token
or -1 if no match is found. |
boolean |
tryMatch(String expected)
Return true if the
current token is equal to the argument. |
boolean |
tryMatchIgnoreCase(String expected)
Return true if the
current token is equal to the argument. |
IllegalStateException |
unexpectedToken(String expected)
Get an exception indicating that the current token was unexpected.
|
IllegalStateException |
unexpectedToken(String expected,
Throwable cause)
Get an exception indicating that the current token was unexpected.
|
public SimpleTextParser(Reader reader)
reader
- reader instance to read characters frompublic SimpleTextParser(CharReadBuffer buffer)
buffer
- read buffer to read characters frompublic int getLineNumber()
public void setLineNumber(int lineNumber)
getLineNumber()
.lineNumber
- line number to set; line numbers start at 1public int getColumnNumber()
readChar()
. The first
character of each line has a column number of 1.public void setColumnNumber(int column)
getColumnNumber()
.column
- the column number to set; column numbers start at 1public int getMaxStringLength()
public void setMaxStringLength(int maxStringLength)
maxStringLength
- maximum length for strings returned by this instanceIllegalArgumentException
- if the argument is less than zeropublic String getCurrentToken()
nextXXX()
methods. This value will be null if no token has yet been read or if the end of content has
been reached.next(int)
,
next(IntPredicate)
,
nextLine()
,
nextAlphanumeric()
public boolean hasNonEmptyToken()
getCurrentToken()
public int getCurrentTokenLineNumber()
getCurrentToken()
public int getCurrentTokenColumnNumber()
getCurrentToken()
public int getCurrentTokenAsInt()
IllegalStateException
- if no token has been read or the
current token cannot be parsed as an integerpublic double getCurrentTokenAsDouble()
IllegalStateException
- if no token has been read or the
current token cannot be parsed as a doublepublic boolean hasMoreCharacters()
UncheckedIOException
- if an I/O error occurspublic boolean hasMoreCharactersOnLine()
UncheckedIOException
- if an I/O error occurspublic int readChar()
current token
.UncheckedIOException
- if an I/O error occurspeekChar()
public SimpleTextParser next(int len)
len
characters from the stream and
set it as the current token. Characters are added to the string until the string
has the specified length or the end of the stream is reached. The characters are
consumed from the stream. The token is set to null if no more characters are available
from the character stream when this method is called.len
- the maximum length of the extracted stringIllegalArgumentException
- if len
is less than 0 or greater than the
configured maximum string length
UncheckedIOException
- if an I/O error occursgetCurrentToken()
,
consume(int, IntConsumer)
public SimpleTextParser nextWithLineContinuation(char lineContinuationChar, int len)
len
characters from the stream and
set it as the current token. This is similar to next(int)
but with the exception
that new line sequences beginning with lineContinuationChar
are skipped.lineContinuationChar
- character used to indicate skipped new line sequenceslen
- the maximum length of the extracted stringIllegalArgumentException
- if len
is less than 0 or greater than the
configured maximum string length
UncheckedIOException
- if an I/O error occursgetCurrentToken()
,
consumeWithLineContinuation(char, int, IntConsumer)
public SimpleTextParser next(IntPredicate pred)
readChar()
will return either a character
that fails the predicate test or -1 if the end of the stream has been reached.
The token will be null if the end of the stream has been reached prior to the method call.pred
- predicate function passed characters read from the input; reading continues
until the predicate returns falseIllegalStateException
- if the length of the produced string exceeds the configured
maximum string length
UncheckedIOException
- if an I/O error occursgetCurrentToken()
,
consume(IntPredicate, IntConsumer)
public SimpleTextParser nextWithLineContinuation(char lineContinuationChar, IntPredicate pred)
next(IntPredicate)
but with the exception
that new line sequences prefixed with lineContinuationChar
are skipped.lineContinuationChar
- character used to indicate skipped new line sequencespred
- predicate function passed characters read from the input; reading continues
until the predicate returns falseIllegalStateException
- if the length of the produced string exceeds the configured
maximum string length
UncheckedIOException
- if an I/O error occursgetCurrentToken()
,
consume(IntPredicate, IntConsumer)
public SimpleTextParser nextLine()
IllegalStateException
- if the length of the produced string exceeds the configured
maximum string length
UncheckedIOException
- if an I/O error occursgetCurrentToken()
public SimpleTextParser nextAlphanumeric()
IllegalStateException
- if the length of the produced string exceeds the configured
maximum string length
UncheckedIOException
- if an I/O error occursgetCurrentToken()
public SimpleTextParser discard(int len)
len
number of characters from the character stream. The
parser position is updated but the current token is not changed.len
- number of characters to discardUncheckedIOException
- if an I/O error occurspublic SimpleTextParser discardWithLineContinuation(char lineContinuationChar, int len)
len
number of characters from the character stream. The
parser position is updated but the current token is not changed. Lines beginning
with lineContinuationChar
are skipped.lineContinuationChar
- character used to indicate skipped new line sequenceslen
- number of characters to discardUncheckedIOException
- if an I/O error occurspublic SimpleTextParser discard(IntPredicate pred)
readChar()
will return either a character that fails the predicate test or -1
if the end of the stream has been reached. The parser position is updated but the current
token is not changed.pred
- predicate test for characters to discardUncheckedIOException
- if an I/O error occurspublic SimpleTextParser discardWithLineContinuation(char lineContinuationChar, IntPredicate pred)
lineContinuationChar
are skipped. The next call o readChar()
will return either a character that fails the predicate test or -1 if the end of the stream
has been reached. The parser position is updated but the current token is not changed.lineContinuationChar
- character used to indicate skipped new line sequencespred
- predicate test for characters to discardUncheckedIOException
- if an I/O error occurspublic SimpleTextParser discardWhitespace()
readChar()
will return either a non-whitespace
character or -1 if the end of the stream has been reached. The parser position is updated
but the current token is not changed.UncheckedIOException
- if an I/O error occurspublic SimpleTextParser discardLineWhitespace()
readChar()
will return either a non-whitespace character on the current line,
the newline character sequence (indicating the end of the line), or -1 (indicating the
end of the stream). The parser position is updated but the current token is not changed.UncheckedIOException
- if an I/O error occurspublic SimpleTextParser discardNewLineSequence()
UncheckedIOException
- if an I/O error occurspublic SimpleTextParser discardLine()
readChar()
will return either the
first character on the next line or -1 if the end of the stream has been reached.
The parser position is updated but the current token is not changed.UncheckedIOException
- if an I/O error occurspublic SimpleTextParser consume(IntPredicate pred, IntConsumer consumer)
consumer
while the given predicate
returns true. The operation ends when the predicate returns false or the end of the stream is
reached.pred
- predicate test for characters to consumeconsumer
- object to be passed each consumed characterUncheckedIOException
- if an I/O error occurspublic SimpleTextParser consumeWithLineContinuation(char lineContinuationChar, int len, IntConsumer consumer)
len
characters from the stream, passing each to the given consumer.
This method is similar to consume(int, IntConsumer)
with the exception that new line
sequences prefixed with lineContinuationChar
are skipped.lineContinuationChar
- character used to indicate skipped new line sequenceslen
- number of characters to consumeconsumer
- function to be passed each consumed characterUncheckedIOException
- if an I/O error occurspublic SimpleTextParser consume(int len, IntConsumer consumer)
len
characters from the stream, passing each to the given consumer.
The operation continues until len
number of characters have been read or the end of
the stream has been reached.len
- number of characters to consumeconsumer
- object to be passed each consumed characterUncheckedIOException
- if an I/O error occurspublic SimpleTextParser consumeWithLineContinuation(char lineContinuationChar, IntPredicate pred, IntConsumer consumer)
consumer
while the given predicate
returns true. This method is similar to consume(IntPredicate, IntConsumer)
with the
exception that new lines sequences beginning with lineContinuationChar
are skipped.lineContinuationChar
- character used to indicate skipped new line sequencespred
- predicate test for characters to consumeconsumer
- object to be passed each consumed characterUncheckedIOException
- if an I/O error occurspublic int peekChar()
UncheckedIOException
- if an I/O error occursreadChar()
public String peek(int len)
len
characters from the stream but
without changing the parser position. Characters are added to the string until the
string has the specified length or the end of the stream is reached.len
- the maximum length of the returned stringlen
characters from the stream
or null if the parser has already reached the end of the streamIllegalArgumentException
- if len
is less than 0 or greater than the
configured maximum string length
UncheckedIOException
- if an I/O error occursnext(int)
public String peek(IntPredicate pred)
pred
- predicate function passed characters read from the input; reading continues
until the predicate returns falsepred
or null if the parser has already
reached the end of the streamIllegalStateException
- if the length of the produced string exceeds the configured
maximum string length
UncheckedIOException
- if an I/O error occursgetCurrentToken()
public SimpleTextParser match(String expected)
current token
with the argument and throw an
exception if they are not equal. The comparison is case-sensitive.expected
- expected tokenIllegalStateException
- if no token has been read or expected
does not exactly
equal the current tokenpublic SimpleTextParser matchIgnoreCase(String expected)
current token
with the argument and throw an
exception if they are not equal. The comparison is not case-sensitive.expected
- expected tokenIllegalStateException
- if no token has been read or expected
does not equal
the current token (ignoring case)public boolean tryMatch(String expected)
current token
is equal to the argument.
The comparison is case-sensitive.expected
- expected tokenIllegalStateException
- if no token has been readUncheckedIOException
- if an I/O error occurspublic boolean tryMatchIgnoreCase(String expected)
current token
is equal to the argument.
The comparison is not case-sensitive.expected
- expected tokenIllegalStateException
- if no token has been readpublic int choose(String... expected)
current token
.
An exception is thrown if no match is found. String comparisons are case-sensitive.expected
- strings to compare with the current tokenIllegalStateException
- if no token has been read or no match is found among the argumentspublic int choose(List<String> expected)
current token
.
An exception is thrown if no match is found. String comparisons are case-sensitive.expected
- strings to compare with the current tokenIllegalStateException
- if no token has been read or no match is found among the argumentspublic int chooseIgnoreCase(String... expected)
current token
,
ignoring case. An exception is thrown if no match is found. String comparisons are not
case-sensitive.expected
- strings to compare with the current tokenIllegalStateException
- if no token has been read or no match is found among the argumentspublic int chooseIgnoreCase(List<String> expected)
current token
,
ignoring case. An exception is thrown if no match is found. String comparisons are not
case-sensitive.expected
- strings to compare with the current tokenIllegalStateException
- if no token has been read or no match is found among the argumentspublic int tryChoose(String... expected)
current token
or -1 if no match is found. String comparisons are case-sensitive.expected
- strings to compare with the current tokenIllegalStateException
- if no token has been readpublic int tryChoose(List<String> expected)
current token
or -1 if no match is found. String comparisons are case-sensitive.expected
- strings to compare with the current tokenIllegalStateException
- if no token has been readpublic int tryChooseIgnoreCase(String... expected)
current token
or -1 if no match is found. String comparisons are not case-sensitive.expected
- strings to compare with the current tokenIllegalStateException
- if no token has been readpublic int tryChooseIgnoreCase(List<String> expected)
current token
or -1 if no match is found. String comparisons are not case-sensitive.expected
- strings to compare with the current tokenIllegalStateException
- if no token has been readpublic IllegalStateException unexpectedToken(String expected)
expected
- string describing what was expectedpublic IllegalStateException unexpectedToken(String expected, Throwable cause)
expected
- string describing what was expectedcause
- cause of the errorpublic IllegalStateException tokenError(String msg)
msg
- error messagepublic IllegalStateException tokenError(String msg, Throwable cause)
msg
- error messagecause
- the cause of the error; may be nullpublic IllegalStateException parseError(String msg)
msg
- error messagepublic IllegalStateException parseError(String msg, Throwable cause)
msg
- error messagecause
- the cause of the error; may be nullpublic IllegalStateException parseError(int line, int col, String msg)
line
- line number of the errorcol
- column number of the errormsg
- error messagepublic IllegalStateException parseError(int line, int col, String msg, Throwable cause)
line
- line number of the errorcol
- column number of the errormsg
- error messagecause
- the cause of the errorpublic static boolean isWhitespace(int ch)
ch
- character (Unicode code point) to testCharacter.isWhitespace(int)
public static boolean isNotWhitespace(int ch)
ch
- character (Unicode code point) to testisWhitespace(int)
public static boolean isLineWhitespace(int ch)
ch
- character (Unicode code point) to testpublic static boolean isNewLinePart(int ch)
ch
- character (Unicode code point) to testpublic static boolean isNotNewLinePart(int ch)
ch
- character (Unicode code point) to testisNewLinePart(int)
public static boolean isAlphanumeric(int ch)
ch
- character (Unicode code point) to testCharacter.isAlphabetic(int)
,
Character.isDigit(int)
public static boolean isNotAlphanumeric(int ch)
ch
- character (Unicode code point) to testisAlphanumeric(int)
public static boolean isIntegerPart(int ch)
digits
ch
- character (Unicode code point) to testpublic static boolean isDecimalPart(int ch)
digits
ch
- character (Unicode code point) to testCopyright © 2016–2021 The Apache Software Foundation. All rights reserved.