public abstract class BaseParser extends Object
The typical usage pattern is to define a sub-class like this...
public class MyParser extends BaseParser {
public MyParser() {
super(20); // Adjust the lookAheadLimit to meet your needs
}
public SomePojo parse(Reader reader) throws IOException, ParseException {
initParse(reader);
SomePojo pojo = new SomePojo();
// Add your parsing logic here.
return pojo;
}
}
| Modifier and Type | Field and Description |
|---|---|
protected int |
columnNumber |
protected int |
lineNumber |
protected int |
lookAheadLimit |
protected PushbackReader |
reader |
| Constructor and Description |
|---|
BaseParser(int lookAheadLimit)
Create a new BaseParser.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
assertEquals(int expected,
int actual)
Assert that a certain expected code point matches the actual code point encountered.
|
protected void |
assertNext(int expected)
Skip over any whitespace, read the next code point, and assert that it matches the expected value.
|
protected void |
assertToken(String expected)
Assert that the reader is positioned at a given expected token.
|
protected void |
assertTokenIgnoreCase(String expected)
Assert that the reader is positioned at a given expected token without regard to case.
|
protected void |
assertWhitespace()
Assert that the next code point is a whitespace character, and skip over any subsequent whitespace characters.
|
protected StringBuilder |
buffer()
Get the internal StringBuilder.
|
protected void |
fail(String message)
Throw a ParseException whose message includes the current lineNumber and columnNumber
where the parse error occurred.
|
protected void |
initParse(Reader reader)
Wrap the given reader in a PushbackReader.
|
protected boolean |
isWhitespace(int c)
Test whether a given code point is white space.
|
protected StringBuilder |
newBuffer()
Get an empty StringBuilder that will be maintained internally.
|
protected int |
next()
Skip any whitespace, and then read the next code point.
|
protected int |
peek()
Look ahead at the next code point in the reader without
advancing the position of the reader.
|
protected int |
read()
Read the next code point and increment the internal line and column counters.
|
protected boolean |
tryToken(String token)
Test whether the reader is positioned at the beginning of a specific lexical token.
|
protected boolean |
tryTokenIgnoreCase(String token)
Test whether the reader is positioned at the beginning of a specific lexical token, without regard to case.
|
protected boolean |
tryWhitespace()
Skip over whitespace.
|
protected void |
unread(int codePoint)
Push the given code point back into the internal reader and decrement the
line number and column number counters if necessary.
|
protected PushbackReader reader
protected int lineNumber
protected int columnNumber
protected int lookAheadLimit
public BaseParser(int lookAheadLimit)
lookAheadLimit - The maximum number of characters that this parser can lookahead while parsing.protected boolean tryWhitespace()
throws IOException
IOExceptionprotected int next()
throws IOException
IOExceptionprotected void assertWhitespace()
throws IOException,
ParseException
IOException - If a error occurred while reading the next code point.ParseException - If the next code point is not a whitespace character.protected StringBuilder newBuffer()
protected void assertNext(int expected)
throws IOException,
ParseException
expected - The expected code point valueIOException - If there was a problem reading the next code point.ParseException - If the next, non-whitespace code point does not match the expected valueprotected void assertEquals(int expected,
int actual)
throws ParseException
expected - The expected code pointactual - The actual code pointParseException - If the expected code point does not match the actual code point.protected StringBuilder buffer()
protected int peek()
throws IOException
IOExceptionprotected void assertToken(String expected) throws IOException, ParseException
expected - The expected lexical token.IOException - If an error is encountered while reading the next token.ParseException - If the reader is not positioned at the expected lexical token.protected void assertTokenIgnoreCase(String expected) throws IOException, ParseException
expected - The expected lexical token.IOException - If an error is encountered while reading the next token.ParseException - If the reader is not positioned at the expected lexical token.protected boolean tryToken(String token) throws IOException
token - The token to be evaluated.IOExceptionprotected boolean tryTokenIgnoreCase(String token) throws IOException
token - The token to be evaluated.IOExceptionprotected boolean isWhitespace(int c)
c - The code point to be testedprotected void fail(String message) throws ParseException
message - The message to include in the ParseException. This message will be prefixed with a
string of the form 'At (lineNumber:columnNumber) -'ParseExceptionprotected void unread(int codePoint)
throws IOException
codePoint - The code point to be pushed back into the internal readerIOExceptionprotected void initParse(Reader reader)
reader - The reader to be wrapped.protected int read()
throws IOException
IOExceptionCopyright © 2019. All rights reserved.